package com.gxa.worker.recycling.controller;


import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.pojo.Recycler;
import com.gxa.core.recycling.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/workLogin")
@Api(tags = "回收员登录接口")
public class LoginController {


    @PostMapping("/login")
    @ApiOperation("回收员登录")
    public Map login(@RequestParam("token") String token) throws URISyntaxException {

        RestTemplate restTemplate = new RestTemplate();
        //指定uri地址
        URI uri = new URI("http://47.108.224.187:8090/sso/findRecyclerByToken");
        // 参数, 用Spring封装的MultiValueMap
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("token", token);
        //调用postForObject方法, 指定uri/参数/返回值类型
        // 返回值如果是一个json字符串, 就可以用JsonUtil来解析
//        String responseString  = restTemplate.postForObject(uri, paramMap, String.class);
//        //把json字符串转换为User对象
//        Recycler recycler = JsonUtil.readToObject(responseString, Recycler.class);


        Map<String, Object> map = restTemplate.postForObject(uri, paramMap, Map.class);
        JsonResult jsonResult = new JsonResult();
        Object code = map.get("code");
        if (code == null) {
            jsonResult.setData(map);
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功");
        } else {
            jsonResult.setCode("100");
            jsonResult.setMsg(map.get("msg").toString());
        }

        return jsonResult.getDataMap();
    }

}
